import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hello_flutter/provider_demo/provider/user_provider.dart';
import 'package:provider/provider.dart';
import 'provider/counter_provider.dart';

/// This is a reimplementation of the default Flutter application using provider + [ChangeNotifier].

//  runApp(
//    /// Providers are above [MyApp] instead of inside it, so that tests
//    /// can use [MyApp] while mocking the providers
//    MultiProvider(
//      providers: [
//        ChangeNotifierProvider(create: (_) => Counter()),
//      ],
//      child: MyApp(),
//    ),
//  );
//}

//class MyApp extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    return const MaterialApp(
//      home: SecondPage(),
//    );
//  }
//}

class SecondPage extends StatelessWidget {
  const SecondPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<CounterProvider> (
        create: (_) => CounterProvider(),
        child: Consumer<CounterProvider>(
          builder: (_, provider, __) {
            return Scaffold(
                appBar: AppBar(
                  title: const Text('Example'),
                ),
                body: Center(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      FlatButton(
                          onPressed: (){
                            context.read<UserProvider>().setName("Edgar");
                            Navigator.pop(context);
                          },
                          child: Text("Edgar,立即登录")
                      ),
                      const Text('You have pushed the button this many times:'),

                      /// Extracted as a separate widget for performance optimization.
                      /// As a separate widget, it will rebuild independently from [SecondPage].
                      ///
                      /// This is totally optional (and rarely needed).
                      /// Similarly, we could also use [Consumer] or [Selector].
                      Count(provider.count),
                    ],
                  ),
                ),
                floatingActionButton: FloatingActionButton(
                /// Calls `context.read` instead of `context.watch` so that it does not rebuild
                /// when [Counter] changes.
                  onPressed: () => provider.increment(), // context.read<CounterProvider>().increment(),
                  tooltip: 'Increment',
                  child: const Icon(Icons.add)
                )
            );
          },
        ),
      );
  }
}

class Count extends StatelessWidget {
  int _count;
  Count(this._count, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(
        /// Calls `context.watch` to make [SecondPage] rebuild when [Counter] changes.
//        '${context.watch<CounterProvider>().count}',
        _count.toString(),
        style: Theme.of(context).textTheme.headline4);
  }

  //

}
